﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Assets.Script
{
    class SkinIconSprite : MonoBehaviour, IPointerClickHandler
    {
        SkinData m_data;
        public int unid;
        public GameObject isSelectObj, isBuyObj, zuanshiObj, shipinObj, jiesuoObj;
        public bool isSelectBool = false, isBuyBool = false, iszuanshiBool = false, isshipinBool = false, isjiesuoBool = false;
        public Image showNowSelectImg;
        public Text showNowZuanshiOrShipinTextNum;
        public int zuanshiNum, shipinNowNum, shipinMaxNum;

        public SkinData GetMData()
        {
            return m_data;
        }
        internal void RefureData(SkinData skinData, int j)
        {
            m_data = skinData;
            unid = j;
            showNowSelectImg.sprite = Resources.Load<Sprite>(m_data.icon);
            isSelectBool = m_data.isme;
            isBuyBool = m_data.isbuy;
            iszuanshiBool = m_data.iszuanshi;
            isshipinBool = m_data.ishipin;
            isjiesuoBool = m_data.issuo;
            isSelectObj.SetActive(isSelectBool);
            isBuyObj.SetActive(!isBuyBool);
            zuanshiObj.SetActive(iszuanshiBool);
            shipinObj.SetActive(isshipinBool);
            if (zuanshiObj.activeSelf)
            {
                zuanshiNum = m_data.zuanshiNum;
                showNowZuanshiOrShipinTextNum.text = zuanshiNum.ToString();
            }
            else
            {
                shipinMaxNum = m_data.shipinNum;
                shipinNowNum = 0;
                showNowZuanshiOrShipinTextNum.text = shipinNowNum + "/" + shipinMaxNum;
            }
            jiesuoObj.SetActive(isjiesuoBool);
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (!isjiesuoBool)
            {
                if (!isBuyBool)
                {
                    Debug.Log("未购买");
                    MainPanelModel model = ModelManager.Ins.GetModel(MainPanelModel.m_name) as MainPanelModel;
                    if (m_data.iszuanshi)
                    {
                        if (zuanshiNum > model.GetData().zuanshi)
                        {
                            MessageCenter.Ins.BuredMess(MessId.OpenTipsPanel,"NOT ENOUGH GEMS");
                        }
                        else
                        {
                            MessageCenter.Ins.BuredMess(MessId.RefureCurrencyData, new CurrencyData()
                            {
                                gold = 0,
                                zuanshi = -m_data.zuanshiNum
                            });
                            m_data.isbuy = true;
                            RefureBuyState();
                        }
                    }
                    else
                    {
                        StartCoroutine(PlayeVideo());
                    }
                }
                else
                {
                    MessageCenter.Ins.BuredMess(MessId.SkinIconRefureShow, unid);
                }
            }
            else
            {
                MessageCenter.Ins.BuredMess(MessId.OpenTipsPanel, "PLAY SNACK EVENT MODE");
            }
        }

        IEnumerator PlayeVideo()
        {
            UIManager.Ins.OpenUI(UIName.AdvertisingPanel);
            yield return new WaitForSeconds(5.5f);
            UIManager.Ins.CloseUI(UIName.AdvertisingPanel); 
            shipinNowNum++;
            showNowZuanshiOrShipinTextNum.text = shipinNowNum + "/" + shipinMaxNum;
            if (shipinNowNum >= shipinMaxNum)
            {
                m_data.isbuy = true;
                RefureBuyState();
            }
        }

        private void RefureBuyState()
        {
            isBuyBool = m_data.isbuy;
            isBuyObj.SetActive(!isBuyBool);
        }

        internal void RefuReRelever(bool v)
        {
            m_data.isme = v;
            isSelectBool = m_data.isme;
            isSelectObj.SetActive(isSelectBool);
        }
    }
}
